package com.gopay.common.domain.bank.ccb.cbrmb.res;

import java.io.Serializable;

import org.apache.log4j.Logger;

import com.gopay.common.domain.bank.ccb.cbrmb.ByteData;
import com.gopay.common.util.cbrmb.MsgUtils;

/**
 * 
 * @ClassName: CbRmbQueryRespMsg
 * @Description: 查询报文返回数据
 * @author gd.cui
 * @date 
 */
public class CbRmbQueryRespMsg implements ByteData, Serializable {

    /**
     * @Fields serialVersionUID :
     */
    private static final long serialVersionUID = 1160861497105169291L;

    private static Logger logger = Logger.getLogger(CbRmbQueryRespMsg.class);

    public static final int MSG_QUERY_RESP_LENGTH = 131;


    /**
     * 交易类型 8888
     */
    private String tranType;
    
    /**
     * 通知方标识 建行发起固定-CCBS； 支付机构发起固定-GOPS;
     */
    private String senderId;

    /**
     * 交易流水 唯一标识每一笔通知，yyyyMMddHHmmss+3位序号
     */
    private String tranSn;
    
    /**
     * 返回码 000000---文件获取成功
     */
    private String retCode;
    
    /**
     * 返回说明
     */
    private String retMsg;
   
    
    private byte[] tranTypeByte = new byte[4];

    private byte[] senderIdByte = new byte[4];

    private byte[] tranSnByte = new byte[17];

    private byte[] retCodeByte = new byte[6];

    private byte[] retMsgByte = new byte[100];
    
    public CbRmbQueryRespMsg() {
    }


    public CbRmbQueryRespMsg(byte[] source) {
        initPropertiesByBytes(source);
    }

    @Override
    public byte[] getByteData() {
        covertPropertiesToBytes();
        return MsgUtils.mergeBytes(this.tranTypeByte,this.senderIdByte, 
        		this.tranSnByte, this.retCodeByte, this.retMsgByte);
    }

    private void covertPropertiesToBytes() {
    	
    	byte[] tranTypeSource = this.tranType.getBytes();
        int tranTypeLenth = tranTypeSource.length;
        MsgUtils.copyBytes(tranTypeSource, this.tranTypeByte, 1, tranTypeLenth, 1);
         
        byte[] senderIdSource = this.senderId.getBytes();
        int senderIdLenth = senderIdSource.length;
        MsgUtils.copyBytes(senderIdSource, this.senderIdByte, 1, senderIdLenth, 1);

        byte[] tranSnSource = this.tranSn.getBytes();
        int tranSnLenth = tranSnSource.length;
        MsgUtils.copyBytes(tranSnSource, this.tranSnByte, 1, tranSnLenth, 1);

        byte[] retCodeSource = this.retCode.getBytes();
        int retCodeLenth = retCodeSource.length;
        MsgUtils.copyBytes(retCodeSource, this.retCodeByte, 1, retCodeLenth, 1);

        byte[] retMsgSource = this.retMsg.getBytes();
        int retMsgLenth = retMsgSource.length;
        MsgUtils.copyBytes(retMsgSource, this.retMsgByte, 1, retMsgLenth, 1);

    }

    @Override
    public void initPropertiesByBytes(byte[] source) {
        if (source != null && source.length >= MSG_QUERY_RESP_LENGTH) {
            MsgUtils.copyBytes(source, this.tranTypeByte, 1, 4, 1);
            MsgUtils.copyBytes(source, this.senderIdByte, 5, 4, 1);
            MsgUtils.copyBytes(source, this.tranSnByte, 9, 17, 1);
            MsgUtils.copyBytes(source, this.retCodeByte, 26, 6, 1);
            MsgUtils.copyBytes(source, this.retMsgByte, 32, 100, 1);

            this.senderId = new String(this.senderIdByte);
            this.tranType = new String(this.tranTypeByte);
            this.tranSn = new String(this.tranSnByte);
            this.retCode = new String(this.retCodeByte);
            this.retMsg = new String(this.retMsgByte);
        } else {
            logger.error("init MSG_QUERY_RESP_LENGTH from bytes error: the bytes not enough");
        }
    }

    public String getSenderId() {
        return senderId;
    }

    public void setSenderId(String senderId) {
        this.senderId = senderId;
    }

    public String getTranType() {
        return tranType;
    }

    public void setTranType(String tranType) {
        this.tranType = tranType;
    }


    public String getTranSn() {
        return tranSn;
    }

    public void setTranSn(String tranSn) {
        this.tranSn = tranSn;
    }

    public String getRetCode() {
        return retCode;
    }

    public void setRetCode(String retCode) {
        this.retCode = retCode;
    }

    public String getRetMsg() {
        return retMsg;
    }

    public void setRetMsg(String retMsg) {
        this.retMsg = retMsg;
    }

}